<?php
/*
 *  $Id: index.php 711 2012-06-18 22:02:11Z k42b3.x@googlemail.com $
 *
 * This is an Amun demo application. The demo application showcases and explains
 * functionalities of Amun. The code is released under the GPLv3. More
 * informations about Amun at http://amun.phpsx.org.
 *
 * This app showcases howto implement an OpenId authentication so that every
 * user of an amun instance can login into your app without creating a new
 * account (In fact every user with an OpenId account can login)
 */


// set include path and autoloader
set_include_path(get_include_path() . PATH_SEPARATOR . 'phar://../../lib/psx.phar');

require_once('PSX/Bootstrap.php');

spl_autoload_register('PSX_Bootstrap::autoload');


// initialize objects
$http     = new PSX_Http(new PSX_Http_Handler_Curl());
$self     = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$openid   = new PSX_OpenId($http, $self);

$validate = new PSX_Validate();
$session  = new PSX_Session('oi', $validate);
$post     = new PSX_Post($validate);

try
{
	if(PSX_Base::getRequestMethod() == 'POST')
	{
		$identity = $post->openid_identifier('string', array(new PSX_Filter_Length(3, 256)));
		$returnTo = $self . '?callback=1';

		if(!$validate->hasError())
		{
			$openid->initialize($identity, $returnTo);

			// add sreg extension
			$sreg = new PSX_OpenId_Extension_Sreg(array('nickname'));

			if($openid->hasExtension($sreg->getNs()))
			{
				$openid->add($sreg);
			}

			$openid->redirect();
		}
		else
		{
			throw new PSX_Exception(current($validate->getError()));
		}
	}
	else if(isset($_GET['callback']) && $_GET['callback'] == '1' && !$session->oi_authed)
	{
		if($openid->verify() === true)
		{
			$data     = $openid->getData();
			$identity = PSX_OpenId::normalizeIdentifier((string) $openid->getIdentity()->getLocalId());
			$params   = PSX_OpenId_ProviderAbstract::getExtension($data, PSX_OpenId_Extension_Sreg::NS);

			$session->set('oi_authed', true);
			$session->set('oi_identity', $identity);
			$session->set('oi_nickname', isset($params['nickname']) ? $params['nickname'] : 'Unknown');

			header('Location: ' . $_SERVER['PHP_SELF']);
			exit;
		}
		else
		{
			throw new PSX_Exception('Verification fails');
		}
	}
	else if(isset($_GET['logout']) && $_GET['logout'] == '1')
	{
		$session->destroy();

		header('Location: ' . $_SERVER['PHP_SELF']);
		exit;
	}
}
catch(Exception $e)
{
	$error = $e->getMessage() . '<br /><pre>' . $e->getTraceAsString() . '</pre>';
}

?>
<!DOCTYPE>
<html>
<head>
	<title>OpenId Login</title>
	<style type="text/css">
	body
	{
		font-size:1em;
		color:#222;
		font-family:"Helvetica Neue", Arial, Helvetica, sans-serif;
	}

	h1
	{
		margin:2px;
		padding:2px;
	}

	h1 a
	{
		text-decoration:none;
		color:#222;
	}

	h2
	{
		margin:1px;
		padding:8px;
		background-color:#ccc;
		border-bottom:2px solid #999;
		font-size:0.8em;
	}

	fieldset
	{
		margin:8px 0px;
		border:1px solid #ccc;
	}

	.content
	{
		width:800px;
		margin:0 auto;
		border:1px solid #ccc;
		padding:8px;
		border-radius:6px;
	}
	</style>
</head>
<body>

<div class="content">

	<h1><a href="<?php echo $_SERVER['PHP_SELF']; ?>">OpenID login</a></h1>

	<?php if(isset($error)): ?>

		<fieldset>
			<legend><b>Error</b></legend>
			<p><?php echo $error; ?></p>
		</fieldset>

	<?php endif; ?>

	<?php if($session->oi_authed): ?>

		<p>You have successful authenticated. The following informations are provided from your account. You can also <a href="?logout=1">logout</a> to restart the authentication process.</p>

		<dl>
			<dt><b>Nickname</b></dt>
			<dd><?php echo $session->oi_nickname; ?></dd>
			<dt><b>Identity</b></dt>
			<dd><?php echo $session->oi_identity; ?></dd>
		</dl>

	<?php else: ?>

		<p>Enter as OpenId the url to your Amun website.</p>

		<form method="POST">

		<label><b>OpenId</b>: <input type="text" name="openid_identifier" id="openid_identifier" value="" /></label>

		<input type="submit" value="Login" />

		</form>

	<?php endif; ?>

</div>

</body>
</html>
